package cn.ibizlab.odoo.core.odoo_sale.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Isale_order_option;
import cn.ibizlab.odoo.core.odoo_sale.domain.Sale_order_option;
import cn.ibizlab.odoo.core.odoo_sale.filter.Sale_order_optionSearchContext;
import cn.ibizlab.odoo.core.odoo_sale.service.ISale_order_optionService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[销售选项] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Sale_order_optionServiceImpl implements ISale_order_optionService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Isale_order_optionClientService sale_order_optionClientService;

    @Override
    public void createBatch(List<Sale_order_option> sale_order_options, int batchSize) {

                        //sale_order_optionClientService.createBatch(sale_order_options,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Isale_order_option clientModel = sale_order_optionClientService.createModel();
        clientModel.setId(id);
        sale_order_optionClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Sale_order_option> sale_order_options, int batchSize) {

        //sale_order_optionClientService.updateBatch(sale_order_options,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //sale_order_optionClientService.removeBatch(sale_order_options) ;

    }

    @Override
    public Sale_order_option get(Integer id) {
        Isale_order_option clientModel = sale_order_optionClientService.createModel();
        clientModel.setId(id);
        sale_order_optionClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Sale_order_option get(Sale_order_option sale_order_option) {
        Sale_order_option retsale_order_option = this.get(sale_order_option.getId()) ;
    	BeanCopier copier=BeanCopier.create(retsale_order_option.getClass(), sale_order_option.getClass(), false);
        copier.copy(retsale_order_option, sale_order_option, null);
        return sale_order_option ;
    }

    @Override
    public boolean create(Sale_order_option sale_order_option) {
        boolean bOk = false ;
        Isale_order_option clientModel = this.convert2Model(sale_order_option,null);
        sale_order_optionClientService.create(clientModel) ;
        this.convert2Domain(clientModel,sale_order_option);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Sale_order_option sale_order_option) {
        boolean bOk = false ;
        Isale_order_option clientModel = this.convert2Model(sale_order_option,null);
        sale_order_optionClientService.update(clientModel) ;
        this.convert2Domain(clientModel,sale_order_option) ;
        bOk = true;
        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Sale_order_option> searchDefault(Sale_order_optionSearchContext context) {
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据产品查询
     */
    @Override
    public Page<Sale_order_option> selectByOdoo_product(Integer id){
        Sale_order_optionSearchContext context = new Sale_order_optionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_order_option> selectByOdoo_create(Integer id){
        Sale_order_optionSearchContext context = new Sale_order_optionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Sale_order_option> selectByOdoo_write(Integer id){
        Sale_order_optionSearchContext context = new Sale_order_optionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据销售订单行查询
     */
    @Override
    public Page<Sale_order_option> selectByOdoo_line(Integer id){
        Sale_order_optionSearchContext context = new Sale_order_optionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据销售订单查询
     */
    @Override
    public Page<Sale_order_option> selectByOdoo_order(Integer id){
        Sale_order_optionSearchContext context = new Sale_order_optionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品计量单位查询
     */
    @Override
    public Page<Sale_order_option> selectByOdoo_uom(Integer id){
        Sale_order_optionSearchContext context = new Sale_order_optionSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Sale_order_option> list = new ArrayList<Sale_order_option>();
        Page<cn.ibizlab.odoo.core.client.model.Isale_order_option> clientModelList = sale_order_optionClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Isale_order_option clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Sale_order_option>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Isale_order_option convert2Model(Sale_order_option domain , Isale_order_option model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = sale_order_optionClientService.createModel() ;
        }

         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getPrice_unitDirtyFlag())
            model.setPrice_unit(domain.getPrice_unit());
         if(domain.getQuantityDirtyFlag())
            model.setQuantity(domain.getQuantity());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getSequenceDirtyFlag())
            model.setSequence(domain.getSequence());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getDiscountDirtyFlag())
            model.setDiscount(domain.getDiscount());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getOrder_id_textDirtyFlag())
            model.setOrder_id_text(domain.getOrder_id_text());
         if(domain.getLine_id_textDirtyFlag())
            model.setLine_id_text(domain.getLine_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getUom_id_textDirtyFlag())
            model.setUom_id_text(domain.getUom_id_text());
         if(domain.getUom_idDirtyFlag())
            model.setUom_id(domain.getUom_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getOrder_idDirtyFlag())
            model.setOrder_id(domain.getOrder_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getLine_idDirtyFlag())
            model.setLine_id(domain.getLine_id());
        return model ;
    }

    public Sale_order_option convert2Domain( Isale_order_option model ,Sale_order_option domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Sale_order_option();
        }

        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getPrice_unitDirtyFlag())
            domain.setPrice_unit(model.getPrice_unit());
        if(model.getQuantityDirtyFlag())
            domain.setQuantity(model.getQuantity());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getSequenceDirtyFlag())
            domain.setSequence(model.getSequence());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getDiscountDirtyFlag())
            domain.setDiscount(model.getDiscount());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getOrder_id_textDirtyFlag())
            domain.setOrder_id_text(model.getOrder_id_text());
        if(model.getLine_id_textDirtyFlag())
            domain.setLine_id_text(model.getLine_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getUom_id_textDirtyFlag())
            domain.setUom_id_text(model.getUom_id_text());
        if(model.getUom_idDirtyFlag())
            domain.setUom_id(model.getUom_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getOrder_idDirtyFlag())
            domain.setOrder_id(model.getOrder_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getLine_idDirtyFlag())
            domain.setLine_id(model.getLine_id());
        return domain ;
    }


}
